/*
read configure :
FILE = /tmp/kvs
TYPE = CREATE       LOAD
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#include "sync.h"
#include "defines.h"

void part_write();
void part_read();
const char keys[15][512]={"bb","aa","yy","xx","zz","hello","hell","hill","hum", "hehe", "hoho", "huhu", "hehehe", "hihihi","hub"};

int main()
{
	char str_tmp[512], pathname[512], str_type[512];
	char *g_image;
	INIT_TYPE init_type;
	int i;

	scanf ("%s %s %s", str_tmp, str_tmp, pathname);
	scanf ("%s %s %s", str_tmp, str_tmp, str_type); 

	sync_init(pathname);

	init_type=part_init(str_type);
	g_image=malloc(IMAGE_SIZE);
	idx_init(init_type, g_image);

	if (init_type==INIT_TYPE_CREATE)
	{
		for (i=0; i<7; i++)
			idx_insert(keys[i], strlen(keys[i]), i, i);

		idx_show();
		i=2;
		printf ("delete state:[%d]\n",idx_delete(keys[i],strlen(keys[i])) );
		idx_show();
	}

	printf ("exiting...\n");
	idx_exit(g_image);	
	return 0;
}


INIT_TYPE part_init(char* str_type)
{
	if (strcmp(str_type, "CREATE")==0) return INIT_TYPE_CREATE;
	return INIT_TYPE_LOAD;
}



























void gen(char *p, int size, char c)
{
	int i;
	for (i=0; i<size; i++) p[i]=c;
}

#define PAGE_SIZE	128

void part_write()
{
	int i;
	char *p[4];
	for (i=0; i<4; i++)
	{
		p[i]=malloc(PAGE_SIZE*2);
		gen(p[i], PAGE_SIZE, 'a'+i);
	}
	sync_write_2(0, p, PAGE_SIZE, 3);

	for (i=0; i<4; i++) free(p[i]);
}

void part_read()
{
	char *p[4];
	int i;

	for (i=0; i<4; i++) p[i]=malloc(PAGE_SIZE*2);

	sync_read_2(0, p, PAGE_SIZE, 3);

	for (i=0; i<4; i++) printf ("%d, %c--%c\n", i, p[i][0], p[i][PAGE_SIZE-1]);
	
	for (i=0; i<4; i++) free(p[i]);
}


